<template>
  <div class="">
    <van-button type="primary" @click="logining"> 支付测试</van-button>
<text>openid:{{openid}}</text>
    <van-button>用户当前下单地址</van-button>

  </div>
</template>

<script>
// 例如：import uploadFile from '@/components/uploadFile/uploadFile'
import {getOpenId, getPayData, getPrepayId} from "../../../requests/user";

var $this
export default {
  name: '',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  props: {

  },
  data() {
    // 这里存放数据
    return {
      data: "",
      openid: ""
    }
  },
  // 方法集合
  methods: {
    logining(){
      wx.login({
        success: (res) => {
          if (res.code) {
            getOpenId(res.code).then(res => {
              if (res) {
                $this.openid = res.data.openid

              getPrepayId(res.data.openid).then(res => {
                if (res.status == '200') {
                  getPayData(res.data.data).then(res => {
                    console.log(res)
                    let $data = {
                      "timeStamp": res.data.data.timeStamp,
                      "nonceStr": res.data.data.nonceStr,
                      "package": "prepay_id="+res.data.data.package,
                      "signType": "RSA",
                      "paySign": res.data.data.paySign
                    }
                    this.data = $data
                    console.log($data)

                    if (res.status == '200') {
                      wx.requestPayment
                      (
                        {
                          "timeStamp": res.data.data.timeStamp,
                          "nonceStr": res.data.data.nonceStr,
                          "package": res.data.data.package,
                          "signType": "RSA",
                          "paySign": res.data.data.paySign,
                          "success":function(res){console.log('成功------',res)},
                          "fail":function(res){console.log('失败------',res)},
                          "complete":function(res){console.log('失败c------',res)}
                        }
                      )
                    }
                  })
                }
              })
            }
            })
          }
        }
      })
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    $this = this
  },
}
</script>


